<?php
class Emptosalary_model extends CRM_Model{
	private $typeDocRid;
	public function __construct(){
		parent::__construct();
		$typeDocRow = $this->GetTypeDoc();
		$this->typeDocRid = $typeDocRow->rid; 
	}
	
	public function GetDS(){
		$this->db->select('_documents.rid, 
							_emp_to_salary_headers.date_doc as date_doc, 
							_emp_to_salary_rows._employeers_rid as _employeers_rid,
							_emp_to_salary_rows._currencies_rid as _currencies_rid,
							_emp_to_salary_rows.salary_value as salary_value,
							_emp_to_salary_rows.bdate as bdate,
							CONCAT(_employeers.f_name,\' \',_employeers.s_name,\' \',_employeers.l_name) as employeer_name,
							_currencies.code as currency_code,
							_filials.name as filial_name,
							_documents.modifyDT as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_emp_to_salary_headers', '_emp_to_salary_headers._documents_rid = _documents.rid');
		$this->db->join('_emp_to_salary_rows', '_emp_to_salary_rows._emp_to_salary_headers_rid = _emp_to_salary_headers.rid');
		$this->db->join('_employeers', '_emp_to_salary_rows._employeers_rid = _employeers.rid');
		$this->db->join('_filials', '_employeers._filials_rid = _filials.rid');
		$this->db->join('_currencies', '_emp_to_salary_rows._currencies_rid = _currencies.rid');
		$this->db->where(array('_documents._documents_types_rid'=>$this->typeDocRid));
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $this->ciObject->GetPagingDS($query->result());
		return array();
	}
	
	public function GetEmployeersList(){
		$this->db->select('_employeers.rid, CONCAT(_employeers.f_name,\' \',_employeers.s_name, \' \',_employeers.l_name) as employeer_name');
		$this->db->from('_employeers');
		$this->db->orderby('employeer_name');
		$query = $this->DBGet('_employeers');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetCurrenciesList(){
		$this->db->select('_currencies.rid, _currencies.code as currency_code');
		$this->db->from('_currencies');
		$this->db->orderby('currency_code');
		$query = $this->DBGet('_currencies');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetEditDS(){
		$this->db->select('_documents.rid, 
							_emp_to_salary_headers.date_doc as date_doc, 
							_emp_to_salary_rows._employeers_rid as _employeers_rid,
							_emp_to_salary_rows._currencies_rid as _currencies_rid,
							_emp_to_salary_rows.salary_value as salary_value,
							_emp_to_salary_rows.bdate as bdate,
							CONCAT(_employeers.f_name,\' \',_employeers.s_name,\' \',_employeers.l_name) as employeer_name,
							_currencies.code as currency_code,
							_filials.name as filial_name,
							_documents.modifyDT as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_emp_to_salary_headers', '_emp_to_salary_headers._documents_rid = _documents.rid');
		$this->db->join('_emp_to_salary_rows', '_emp_to_salary_rows._emp_to_salary_headers_rid = _emp_to_salary_headers.rid');
		$this->db->join('_employeers', '_emp_to_salary_rows._employeers_rid = _employeers.rid');
		$this->db->join('_filials', '_employeers._filials_rid = _filials.rid');
		$this->db->join('_currencies', '_emp_to_salary_rows._currencies_rid = _currencies.rid');
		$this->db->where(array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $query->row();
		return False;
	}
	
	public function CreateRecord(){
		$insDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->insert('_documents', $insDocumentArray);
		$docRid = $this->db->insert_id();
		$insHeaderArray = array('date_doc'=>$_POST['date_doc'],
							'_documents_rid'=>$docRid,
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);

		$this->db->insert('_emp_to_salary_headers', $insHeaderArray);
		$insRowsArray = array('_emp_to_salary_headers_rid'=>$this->db->insert_id(),
							'_employeers_rid'=>$_POST['_employeers_rid'],
							'_currencies_rid'=>$_POST['_currencies_rid'],
							'salary_value'=>$_POST['salary_value'],
							'bdate'=>$_POST['bdate'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->insert('_emp_to_salary_rows', $insRowsArray);
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $docRid;
		}		
	}
	
	public function UpdateRecord(){
		$updateDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->update('_documents', $updateDocumentArray, array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
				
		$updateHeaderArray = array('date_doc'=>$_POST['date_doc'],
								'archive'=>$_POST['archive'],
								'modifier_users_rid'=>$this->userRid,
								'modifyDT'=>$this->nowTime);

		$this->db->update('_emp_to_salary_headers', $updateHeaderArray, array('_emp_to_salary_headers._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->db->getwhere('_emp_to_salary_headers', array('_emp_to_salary_headers._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		if(!$query->num_rows()) {
			$this->db->trans_rollback();
    		return False;
		}
		$row = $query->row();
		$updateRowsArray = array('_employeers_rid'=>$_POST['_employeers_rid'],
							'_currencies_rid'=>$_POST['_currencies_rid'],
							'salary_value'=>$_POST['salary_value'],
							'bdate'=>$_POST['bdate'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->update('_emp_to_salary_rows', $updateRowsArray, array('_emp_to_salary_rows._emp_to_salary_headers_rid'=>$row->rid));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $this->ciObject->GetSessionData('activerecord');
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_documents', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
}
?>